package com.dragon.logical.service.auth.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.model.ResultCode;
import com.dragon.common.model.exception.CustomException;
import com.dragon.logical.service.auth.DepartmentService;
import com.dragon.repository.entity.AdminDepartment;
import com.dragon.repository.entity.AdminRole;
import com.dragon.repository.entity.Department;
import com.dragon.repository.entity.RoleDepartment;
import com.dragon.repository.mapper.AdminDepartmentMapper;
import com.dragon.repository.mapper.DepartmentMapper;
import com.dragon.repository.mapper.RoleDepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-06-12
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired
    DepartmentMapper mapper;

    @Autowired
    AdminDepartmentMapper adMapper;

    @Autowired
    RoleDepartmentMapper roleDepartmentMapper;

    @Override
    public Page<Department> getDepPage(Integer page, Integer limit) {
        Page<Department> depPage = new Page<>();
        Page<Department> selectPage = (Page<Department>) mapper.selectPage(depPage, null);
        return selectPage;
    }


    @Override
    public List<Department> selectDepartmentList(String search){
        QueryWrapper<Department> departmentQueryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(search)){
            departmentQueryWrapper.lambda().like(Department::getName,search);
        }
        List<Department> departments = mapper.selectList(departmentQueryWrapper);
        return departments;
    }

    @Override
    public void addDepartment(Department department) {
        Department dep= mapper.selectById(department.getId());
        if (dep != null){
            throw new CustomException(ResultCode.DATA_ALREADY_EXISTS);
        }
        department.setCreateTime(DateUtil.date());
        mapper.insert(department);
    }

    @Override
    public void updateDepartment(Department department) {
        Department dep = mapper.selectById(department.getId());
        if (dep == null){
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        department.setUpdateTime(DateUtil.date());
        mapper.updateById(department);
    }

    @Override
    public void deleteDepartment(Integer id) {
        Department department = mapper.selectById(id);
        if (department == null){
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }

        List<Integer> ids = new ArrayList<>();

        ids.add(id);

        List<AdminDepartment> adminDepartments = adMapper.selectAdminDepartmentByDepartmentIds(ids);
        if (adminDepartments.size() != 0){
            throw new CustomException(ResultCode.USER_DEPARTMENT_IS_EXIT);
        }

        //角色部门
        QueryWrapper<RoleDepartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RoleDepartment::getDepartmentId, department.getId());
        roleDepartmentMapper.delete(queryWrapper);
        mapper.deleteById(id);
    }

    @Override
    public void deleteDepartmentList(List<Integer> ids){
        QueryWrapper<Department> dw = new QueryWrapper<>();
        dw.lambda().in(Department::getId, ids);
        List<Department> departments = mapper.selectList(dw);
        if (ids.size() != departments.size()){
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }

        //用户部门表
        List<AdminDepartment> adminDepartments = adMapper.selectAdminDepartmentByDepartmentIds(ids);
        if (adminDepartments.size() != 0){
            throw new CustomException(ResultCode.USER_DEPARTMENT_IS_EXIT);
        }

        //角色部门表
        QueryWrapper<RoleDepartment> rdQw = new QueryWrapper<>();
        rdQw.lambda().in(RoleDepartment::getDepartmentId,ids);
        roleDepartmentMapper.delete(rdQw);
        mapper.delete(dw);
    }

}
